package com.itheima.advices;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Slf4j
@Component
@Aspect
public class MethodTimeAdviceAop {
    // 1,定义1个方法A,
    // @Around("execution(* com.itheima.service.impl.*.*(..))")
    // public Object getMethodTime(ProceedingJoinPoint pjp){
    //
    //     long begin = System.currentTimeMillis();
    //     Object object = null;
    //     try {
    //         object = pjp.proceed();
    //     } catch (Throwable e) {
    //         throw new RuntimeException(e);
    //     }
    //
    //     long end = System.currentTimeMillis();
    //
    //     // 获取方法签名对象
    //     MethodSignature signature = (MethodSignature) pjp.getSignature();
    //     // 获取方法对象
    //     Method method = signature.getMethod();
    //     // 获取方法名
    //     String name = method.getName();
    //
    //
    //     log.info("执行方法：" + name+ "耗时:" + (end-begin)+"毫秒");
    //
    //     return object;
    // }@annotation(com.itheima.annotation.LogAnnotation)

    // @Pointcut("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
    @Pointcut("@annotation(com.itheima.annotation.LogAnnotation)")
    public void pointCut(){

    }

    @Before("pointCut()")
    public void  before(){

        log.info("执行了before");

    }
    @AfterReturning("pointCut()")
    public void afterReturning(){
       log.info("执行了AfterReturning");
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing(){
        log.info("执行了afterThrowing");
    }

    @After("pointCut()")
    public void after(){
        log.info("执行了after方法");
    }
}
